Two types of data-manipulation:

  1. spatial data-manipulations: spatial cropping, union, aggregation, etc.
  2. ‘regular’ data-manipulations: combine categories, calculate percentage of population, etc.

… but same syntax and in one dataframe.

PS: always an option, do (2) in SAS, Stata, etc. before reading and joining data (previous step).

library(here)
library(sf)
library(tmap)
library(dplyr)
library(readxl)
library(mapview)
# (1) load spatial data
raillines <- st_read(here('data/source/census_1851_raillines/1851EngWalesScotRail_Lines.shp'))

districts_spatial <- st_read(here('data/source/census_1851_districts/1851EngWalesRegistrationDistrict.shp')) %>%
  mutate(CEN1 = as.numeric(as.character(CEN1))) # make sure identifiers are the same type

# (2) load and add data-of-interest
districts_data <- read_excel(here('data/census1851_districts_count.xlsx'))
districts <- left_join(districts_spatial, districts_data, by = c('CEN1' = 'district_id'))

Data-manipulation on joint dataset

districts <- districts %>%
  mutate(pct_prof = tertiary_services_professions / total)
mapview(districts, zcol = 'pct_prof')

Spatial manipulation on joint dataset

Data-exploration: what is R_DIV variable?

divisions <- districts %>%
  group_by(R_DIV) %>%
  tally()
divisions
## Simple feature collection with 11 features and 2 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: 87019.07 ymin: 7067.26 xmax: 655747.5 ymax: 657473.5
## epsg (SRID):    NA
## proj4string:    +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs
## # A tibble: 11 x 3
##    R_DIV           n                                               geometry
##  * <fct>       <int>                                         <GEOMETRY [m]>
##  1 EASTERN       153 MULTIPOLYGON (((576079.7 182724.1, 575881.1 182595.6,…
##  2 LONDON         50 MULTIPOLYGON (((545304.4 182108.8, 545328.8 182054.2,…
##  3 NORTH MIDL…    81 POLYGON ((538173.2 334349.2, 538357.7 334297.6, 53888…
##  4 NORTH WEST…    49 MULTIPOLYGON (((318709.4 387773.5, 318669.2 387723.4,…
##  5 NORTHERN       64 MULTIPOLYGON (((342348.8 478405.1, 342385.8 478359.1,…
##  6 SOUTH EAST…   196 MULTIPOLYGON (((448535.6 96647.07, 448662.4 96607.89,…
##  7 SOUTH MIDL…   111 MULTIPOLYGON (((516697 173206.1, 516698.8 173165.7, 5…
##  8 SOUTH WEST…   146 MULTIPOLYGON (((87825.76 8836.771, 87870.98 8816.206,…
##  9 WELSH          91 MULTIPOLYGON (((322121.3 165180.4, 322145.6 165022.8,…
## 10 WEST MIDLA…   158 MULTIPOLYGON (((359975 172330.1, 359944 172223.4, 359…
## 11 YORKSHIRE      99 MULTIPOLYGON (((446011.8 382146.7, 446017.4 382109, 4…
mapview(divisions)

Spatial / data-manipulation: select districts in Manchester-region

nwestern <- districts %>%
  filter(R_DIV == 'NORTH WESTERN')
# static plot of Manchester districts
qtm(nwestern, fill = 'pct_secondary')

# interactive plot of Manchester districts
mapview(nwestern, zcol = 'pct_secondary') + raillines
map_manchester <- mapview(nwestern, zcol = 'pct_secondary') + raillines
mapshot(map_manchester, url = here('output/map_manchester.html'))
# (1) load spatial data
raillines <- st_read(here('data/source/census_1851_raillines/1851EngWalesScotRail_Lines.shp'))
## Reading layer `1851EngWalesScotRail_Lines' from data source `/home/rstudio/projects/historical-maps-r/data/source/census_1851_raillines/1851EngWalesScotRail_Lines.shp' using driver `ESRI Shapefile'
## Simple feature collection with 1431 features and 2 fields
## geometry type:  LINESTRING
## dimension:      XY
## bbox:           xmin: 155368 ymin: 36976.83 xmax: 655241.1 ymax: 805911.3
## epsg (SRID):    NA
## proj4string:    +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs
districts_spatial <- st_read(here('data/source/census_1851_districts/1851EngWalesRegistrationDistrict.shp')) %>%
  mutate(CEN1 = as.numeric(as.character(CEN1)))
## Reading layer `1851EngWalesRegistrationDistrict' from data source `/home/rstudio/projects/historical-maps-r/data/source/census_1851_districts/1851EngWalesRegistrationDistrict.shp' using driver `ESRI Shapefile'
## Simple feature collection with 1194 features and 6 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 87019.07 ymin: 7067.26 xmax: 655747.5 ymax: 657473.5
## epsg (SRID):    NA
## proj4string:    +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs
## Warning: NAs introduced by coercion
# (2) load and add data-of-interest
districts_data <- read_excel(here('data/census1851_districts_count.xlsx'))
districts <- left_join(districts_spatial, districts_data, by = c('CEN1' = 'district_id'))

# (3) manipulate data (select Manchester area)
nwestern <- districts %>% filter(R_DIV == 'NORTH WESTERN')

# export & share with collaborators, etc.
map_manchester <- mapview(nwestern, zcol = 'pct_secondary') + raillines
mapshot(map_manchester, url = here('output/map_manchester.html'))

Share with collaborators, presentation, e.g. statistical sectors.